﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Runtime.Serialization;
using System.ServiceModel;
using System.ServiceModel.Web;
using System.Text;
using System.ServiceModel.Activation;
using CORSSupport;

namespace RESTSessionService
{
    [ServiceContract]
    public interface IRESTSessionService
    {
        /// <summary>
        /// Opens a session with the server.
        /// </summary>
        /// <param name="creds">Credential string in the following format : 'login;password'.</param>
        /// <returns>A string of the following format :
        ///     !;Error message, or
        ///     .;Token
        ///     The format depends on the occuring of an error or not. The tokeb MUST be used
        ///     in latter calls to designate the session newly openned.</returns>
        [OperationContract]
        [WebGet(ResponseFormat = WebMessageFormat.Json,
                   RequestFormat = WebMessageFormat.Json,
                   BodyStyle = WebMessageBodyStyle.Bare,
                   UriTemplate = "login?Creds={creds}"), CorsEnabled]
        string OpenSession(string creds);

        /// <summary>
        /// Utility methode that keeps a session alive.
        /// </summary>
        /// <param name="token">The session token.</param>
        /// <returns>Returns true if the session was renewed, false if it is closed.</returns>
        [OperationContract]
        [WebGet(ResponseFormat = WebMessageFormat.Json,
           RequestFormat = WebMessageFormat.Json,
           BodyStyle = WebMessageBodyStyle.Bare,
           UriTemplate = "refresh?token={token}"), CorsEnabled]
        bool RefreshSession(string token);

        /// <summary>
        /// Closes a session.
        /// </summary>
        /// <param name="token">The session token.</param>
        [OperationContract]
        [WebGet(ResponseFormat = WebMessageFormat.Json,
                   RequestFormat = WebMessageFormat.Json,
                   BodyStyle = WebMessageBodyStyle.Bare,
                   UriTemplate = "close?token={token}"), CorsEnabled]
        void CloseSession(string token);

        /// <summary>
        /// Creates a new user to the database with given parameters.
        /// Duplicate logins or mail are forbidden.
        /// </summary>
        /// <param name="mail">The user's mail. Duplicates will be checked.</param>
        /// <param name="login">The user's desired login. Duplicates will be checked.</param>
        /// <param name="pass">The uer's password. Will be MD5'd.</param>
        /// <returns>Returns an error message in case of failure, nothing if everything went right.</returns>
        [OperationContract]
        [WebGet(ResponseFormat = WebMessageFormat.Json,
                   RequestFormat = WebMessageFormat.Json,
                   BodyStyle = WebMessageBodyStyle.Bare,
                   UriTemplate = "create?mail={mail}&login={login}&pass={pass}&race={race}"), CorsEnabled]
        string CreateUser(string mail, string login, string pass, string race);

        /// <summary>
        /// Regenerate the user's password and sends it to it's mail adress.
        /// </summary>
        /// <param name="login">The user's login.</param>
        /// <param name="mail">The mail of the user.</param>
        /// <returns>Returns an error message in case of failure, nothing if everything went right.</returns>
        [OperationContract]
        [WebGet(ResponseFormat = WebMessageFormat.Json,
                   RequestFormat = WebMessageFormat.Json,
                   BodyStyle = WebMessageBodyStyle.Bare,
                   UriTemplate = "lost?login={login}&mail={mail}"), CorsEnabled]
        string RegeneratePass(string login, string mail);
        
    }


}
